<template>
  <div id="userRegisterPage">
    <h2 class="title">智能渔业资源可视化大数据分析 - 用户注册</h2>
    <div class="desc">企业级智能大数据分析</div>
    <a-form
      :model="formState"
      name="basic"
      label-align="left"
      autocomplete="off"
      @finish="handleSubmit"
    >
      <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
        <a-input v-model:value="formState.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码' },
          { min: 8, message: '密码不能小于 8 位' },
        ]"
      >
        <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" />
      </a-form-item>
      <a-form-item
        name="checkPassword"
        :rules="[
          { required: true, message: '请输入确认密码' },
          { min: 8, message: '确认密码不能小于 8 位' },
        ]"
      >
        <a-input-password v-model:value="formState.checkPassword" placeholder="请输入确认密码" />
      </a-form-item>
      <div class="tips">
        已有账号？
        <RouterLink to="/user/login" @click="handleLogin">去登录</RouterLink>
      </div>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">注册</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { userApi } from '@/api';
import { message } from 'ant-design-vue';

const router = useRouter();
const formState = ref({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
});

const handleSubmit = async () => {
  const res = await userApi.userRegister({
    userName: formState.value.userAccount,
    password: formState.value.userPassword,
    checkPassword: formState.value.checkPassword,
  });

  if (res.code === 0) {
    message.success('注册成功');
    setTimeout(() => {
      router.push('/user/login');
    }, 1000);
  } else {
    message.error('注册失败:' + res.message);
  }
};
</script>

<style scoped>
#userRegisterPage { 
  min-width: 375px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 1px solid #ccc; 
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3);
}

#userRegisterPage:hover {   
  top: 48%;
}

.title {
  text-align: center;
  margin-bottom: 16px;  
}

.desc {
  text-align: center;
  color: #bbb;
  margin-bottom: 16px;    
}

.tips {
  margin-bottom: 16px;
  color: #bbb;
  font-size: 13px;  
  text-align: right;
}
</style>

